package com.bwie.parkdemo.mvc;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;

import com.bwie.lib_core.action.ActActionField;
import com.bwie.parkdemo.R;
import com.bwie.parkdemo.mvvm.ui.activity.HistorySignActivity;
import com.bwie.parkdemo.mvvm.ui.activity.ParkingAllActivity;
import com.bwie.parkdemo.mvvm.ui.activity.PatrolesActivity;
import com.bwie.parkdemo.mvvm.ui.activity.RepairActivity;
import com.bwie.parkdemo.mvvm.ui.activity.SelAllApplyActivity;
import com.bwie.parkdemo.mvvm.ui.activity.SysSignActivity;
import com.bwie.parkdemo.mvvm.ui.activity.VisitorActivity;
import com.bwie.parkdemo.widget.WebViewJavaScriptFunction;
import com.bwie.parkdemo.widget.X5WebView;
import com.jaeger.library.StatusBarUtil;

/**
 * 软件主界面->显示服务器返回的浏览器界面
 * */
public class MainActivity extends AppCompatActivity {

    private String url;
    private X5WebView main_x5web;
    private MainWebViewJavaScriptFunction listener;
    //腾讯X5浏览器js监听->监听捕获H5页面中的js事件

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //导入沉浸式状态栏颜色
        StatusBarUtil.setColor(this,getResources().getColor(com.bwie.lib_core.R.color.statuescolor),0);
//        url = getIntent().getBundleExtra(ActActionField.ACTBUNDLE).getString(ActActionField.ACTBUNDLE);
//        Log.e("ZXY","MainActivity:"+url);
        url = "http://172.81.227.127:8066/index.html";
        init();
    }

    private void init(){
        listener = new MainWebViewJavaScriptFunction();
        main_x5web = findViewById(R.id.main_x5web);
        main_x5web.loadUrl(url);//加载首页
        main_x5web.getView().setOverScrollMode(View.OVER_SCROLL_ALWAYS);
        main_x5web.addJavascriptInterface(listener,"androidinfo");//listener监听器,s:要监听的名称->h5开发人员进行约定
    }

    //封装界面跳转方法
    private void start(Class clz){
        Intent intent = new Intent(this,clz);
        startActivity(intent);
    }

    private class MainWebViewJavaScriptFunction implements WebViewJavaScriptFunction {

        @Override
        public void onJsFunctionCalled(String tag) {
        }
        //注册要监听的js事件
        @JavascriptInterface
        public void androidparking(String id){
            start(ParkingAllActivity.class);
        }
        @JavascriptInterface
        public void androidapply(String id){
            Log.e("ZXY","androidparking:"+id);
            start(SelAllApplyActivity.class);
        }
        @JavascriptInterface
        public void androidvisit(String id){
            start(VisitorActivity.class);
            Log.e("ZXY","androidparking:"+id);
        }
        @JavascriptInterface
        public void androidpatrol(String id){
            Log.e("ZXY","androidparking:"+id);
            start(PatrolesActivity.class);
        }
        @JavascriptInterface
        public void androidrepair(String id){
            Log.e("ZXY","androidparking:"+id);
            start(RepairActivity.class);
        }
        @JavascriptInterface
        public void androidaddrepair(String id){
            Log.e("ZXY","androidparking:"+id);
        }
        @JavascriptInterface
        public void androidaddculture(String id){
            Log.e("ZXY","androidparking:"+id);
        }
        @JavascriptInterface
        public void androidaddnotice(String id){
            Log.e("ZXY","androidparking:"+id);
        }
        @JavascriptInterface
        public void androidculture(String id){
            Log.e("ZXY","androidparking:"+id);
        }
        @JavascriptInterface
        public void androidnotice(String id){
            Log.e("ZXY","androidparking:"+id);
        }
        @JavascriptInterface
        public void androidsign(String id){
            Log.e("ZXY","androidparking:"+id);
            start(SysSignActivity.class);
        }
        @JavascriptInterface
        public void androidattendance(String id){
            Log.e("ZXY","androidparking:"+id);
            start(HistorySignActivity.class);
        }
        @JavascriptInterface
        public void androidproperty(String id){
            Log.e("ZXY","androidparking:"+id);
        }
        @JavascriptInterface
        public void androidcheckculture(String id){
            Log.e("ZXY","androidparking:"+id);
        }
        @JavascriptInterface
        public void androidpeople(String id){
            Log.e("ZXY","androidparking:"+id);
        }
    }

}
